package net.chrisrichardson.ec2deployer.cluster


import net.chrisrichardson.ec2deployer.core.AWSProperties
import net.chrisrichardson.ec2deployer.core.EC2
import net.chrisrichardson.ec2deployer.core.EC2InstanceType
import net.chrisrichardson.ec2deployer.testutil.AWSPropertiesUtil;


class ClusterIntegrationTest extends GroovyTestCase {

    def awsProperties
    def ec2

    def explodedWar

    void setUp() {
        AWSProperties awsProperties = AWSPropertiesUtil.makeAWSProperties()
        ec2 = new EC2(awsProperties)
        explodedWar = locatePath(['h:/cer/code/j2eebook/projecttrack/webapp/target/ptrack', 'h:/cer/code/j2eebook/sptrack/webapp/target/ptrack'])
    }

    def locatePath(possibleLocations) {
        // :-(
        def location = possibleLocations.grep {new File(it).exists()}[0]
        if (location)
            return location
        fail("Can't find this file: " + location)
    }

    void testMultiInstanceCluster() {
        ClusterSpec clusterSpec = new ClusterSpec().tomcats(1).instanceType(EC2InstanceType.SMALL).slaves(1).webApp(explodedWar, "ptrack")
                .catalinaOptsBuilder({builder, databasePrivateDnsName, slaveNames ->
            builder.arg("-Xmx500m")
            builder.prop("com.sun.management.jmxremote")
            builder.prop("com.sun.management.jmxremote.port", 8091)
            builder.prop("com.sun.management.jmxremote.authenticate", false)
            builder.prop("com.sun.management.jmxremote.ssl", false)
            builder.prop("ptrack.application.environment", "ec2")
            builder.prop("log4j.configuration", "log4j-minimal.properties")
            builder.prop("jdbc.db.server", databasePrivateDnsName)}).schema("ptrack", ["ptrack": "ptrack"], ["src/test/resources/testdml1.sql", "src/test/resources/testdml2.sql"])
        SimpleCluster cluster = new SimpleCluster(ec2, clusterSpec)

        cluster.start(true)

        cluster.dumpInfo()

        def jmeterTest = locatePath(["h:/cer/code/j2eebook/projecttrack/functionalTests/jmeter/SimpleTest.jmx", "h:/cer/code/j2eebook/sptrack/functionalTests/jmeter/SimpleTest.jmx"])
        cluster.loadTest(jmeterTest, [1], [])

        cluster.stop()
    }

    void testSingleInstanceCluster() {
        ClusterSpec clusterSpec = new ClusterSpec().tomcats(1).instanceType(EC2InstanceType.SMALL).slaves(1).webApp(explodedWar, "ptrack")
                .catalinaOptsBuilder({builder, databasePrivateDnsName, slaveNames ->
            builder.arg("-Xmx500m")
            builder.prop("ptrack.application.environment", "ec2")
            builder.prop("log4j.configuration", "log4j-minimal.properties")
            builder.prop("jdbc.db.server", databasePrivateDnsName)}).schema("ptrack", ["ptrack": "ptrack"], ["src/test/resources/testdml1.sql", "src/test/resources/testdml2.sql"])
        clusterSpec.topology(SingleInstanceTopology.simpleName)

        SimpleCluster cluster = new SimpleCluster(ec2, clusterSpec)

        cluster.start(true)

        cluster.dumpInfo()

        def jmeterTest = locatePath(["h:/cer/code/j2eebook/projecttrack/functionalTests/jmeter/SimpleTest.jmx", "h:/cer/code/j2eebook/sptrack/functionalTests/jmeter/SimpleTest.jmx"])
        cluster.loadTest(jmeterTest, [1], [])

        cluster.stop()
    }

    void testSingleInstanceClusterWithHASlave() {
        ClusterSpec clusterSpec = new ClusterSpec().tomcats(1).instanceType(EC2InstanceType.SMALL).slaves(1).webApp(explodedWar, "ptrack")
                .catalinaOptsBuilder({builder, databasePrivateDnsName, slaveNames ->
            builder.arg("-Xmx500m")
            builder.prop("ptrack.application.environment", "ec2")
            builder.prop("log4j.configuration", "log4j-minimal.properties")
            builder.prop("jdbc.db.server", databasePrivateDnsName)}).schema("ptrack", ["ptrack": "ptrack"], ["src/test/resources/testdml1.sql", "src/test/resources/testdml2.sql"])
        clusterSpec.topology(SingleInstanceTopology.simpleName)
        clusterSpec.haSlave()

        SimpleCluster cluster = new SimpleCluster(ec2, clusterSpec)

        cluster.start(true)

        cluster.dumpInfo()

        def jmeterTest = locatePath(["h:/cer/code/j2eebook/projecttrack/functionalTests/jmeter/SimpleTest.jmx", "h:/cer/code/j2eebook/sptrack/functionalTests/jmeter/SimpleTest.jmx"])
        cluster.loadTest(jmeterTest, [1], [])

        cluster.stop()
    }
}
